
#include "../../../inc/Ui/GoogleBuzz/GoogleBuzzNewActivity.h"
#include "../../../inc/Ui/GoogleBuzz/GoogleBuzzMenuForm.h"
using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;


GoogleBuzzNewActivity::GoogleBuzzNewActivity(void)
{
}

GoogleBuzzNewActivity::~GoogleBuzzNewActivity(void)
{
}

bool
GoogleBuzzNewActivity::Initialize()
{
	Form::Construct(L"GoogleBuzzNewActivity");

	return true;
}

result
GoogleBuzzNewActivity::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_CANCEL);

	pEditField = static_cast<EditField*>(GetControl(L"IDC_Title"));
	pEditArea = static_cast<EditArea*>(GetControl(L"IDC_Content"));
	return r;
}

result
GoogleBuzzNewActivity::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void GoogleBuzzNewActivity::OnActionPerformed (const Osp::Ui::Control &source, int actionId){
	switch(actionId){
	case ID_SOFTKEY_OK:
		ProcessSoftKeyOk();
		break;

	case ID_SOFTKEY_CANCEL:
		ProcessSoftKeyCancel();
		break;
	}
}

void GoogleBuzzNewActivity::ProcessSoftKeyOk(){
	Application *pApp = Application::GetInstance();



	String err = L"";
	if (pEditField->GetTextLength() <= 0){
		result r = GetLastResult();
		AppLog(GetErrorMessage(r));
		pApp->GetAppResource()->GetString(L"IDS_GOOGLEBUZZ_ADD_ACTIVITY_ERROR_TITLE", err);
	}
	if (pEditArea->GetTextLength() <= 0){
		String temp = L"";
		pApp->GetAppResource()->GetString(L"IDS_GOOGLEBUZZ_ADD_ACTIVITY_ERROR_CONTENT", temp);
		err.Append('\n');
		err.Append(temp);
	}

	if (err.GetLength() == 0){
		ArrayList *pListArgs = new ArrayList();
		pListArgs->Construct();

		String title = pEditField->GetText();

		String content = pEditArea->GetText();

		pListArgs->Add(*(new String(title)));
		pListArgs->Add(*(new String(content)));

		FormManager *pFormManager = null;
		FormManager::GetInstance(pFormManager);

		Form *pForm = null;
		pFormManager->GetForm(GoogleBuzzMenuForm::GetFormId(), pForm);
		pForm->SendUserEvent(GoogleBuzzMenuForm::REQUEST_ADD_ACTIVITY_OK, pListArgs);
		pFormManager->SetCurrentForm(GoogleBuzzMenuForm::GetFormId());
	}else{
		MessageBox message;
		message.Construct(L"Google buzz", err, MSGBOX_STYLE_OK, 30000);
		int i = 0;
		message.ShowAndWait(i);
	}
}

void GoogleBuzzNewActivity::ProcessSoftKeyCancel(){
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);

	Form *pForm = null;
	pFormManager->GetForm(GoogleBuzzMenuForm::GetFormId(), pForm);
	pForm->SendUserEvent(GoogleBuzzMenuForm::REQUEST_ADD_ACTIVITY_CANCEL, null);
	pFormManager->SetCurrentForm(GoogleBuzzMenuForm::GetFormId());
}

